#include "pluginsmanagerpooldao.h"
#include "../ConnectionPool.h"

#include <iostream>

/* static */
QCache<QString, PluginsManagerPool> PluginsManagerPoolDao::pluginsManagerPoolCache;
QCache<QString, QList<PluginsManagerPool>> PluginsManagerPoolDao::pluginsManagerPoolsCache;

PluginsManagerPoolDao::PluginsManagerPoolDao()
{

}

/* 根据id查找 */
PluginsManagerPool *PluginsManagerPoolDao::findOne(const quint32 &id)
{
	/* 缓存 */
	QString key = QString("findOne%1").arg(id);
	if (pluginsManagerPoolCache.contains(key))
		return pluginsManagerPoolCache.object(key);

	/* 组合sql语句 */
	QString content = QString("select * from `plugins_manager_pool` where id = %1").arg(id);

	//获得查询结果
	QSqlRecord* rec = ConnectionPool::runOne(content);

	//判空
	if(rec == nullptr) return nullptr;

	//将查询的结果存入PluginsManagerPool类的对象中
	PluginsManagerPool* pluginsManagerPool = new PluginsManagerPool();
	pluginsManagerPool->setId(rec->value("id").toUInt());
	pluginsManagerPool->setPid(rec->value("pid").toUInt());
	pluginsManagerPool->setPmid(rec->value("pmid").toUInt());
	pluginsManagerPool->setAbsFilename(rec->value("abs_filename").toString());
	pluginsManagerPool->setFilename(rec->value("filename").toString());

	/* 加入缓存 */
	pluginsManagerPoolCache.insert(key, pluginsManagerPool);

	return pluginsManagerPool;
}

/* 根据自定义条件查找 */
PluginsManagerPool *PluginsManagerPoolDao::findOne(const Specification &spec)
{
	/* 缓存 */
	if (pluginsManagerPoolCache.contains("findOne" + spec.getSqlWhere()))
		return pluginsManagerPoolCache.object("findOne" + spec.getSqlWhere());

	//构建sql语句
	QString content = QString("select * from `plugins_manager_pool` where %1").arg(spec.getSqlWhere());

	//获得查询结果
	QSqlRecord* rec = ConnectionPool::runOne(content);

	//判空
	if(rec == nullptr) return nullptr;

	//将查询的结果存入PluginsManagerPool类的对象中
	PluginsManagerPool* pluginsManagerPool = new PluginsManagerPool;
	pluginsManagerPool->setId(rec->value("id").toUInt());
	pluginsManagerPool->setPid(rec->value("pid").toUInt());
	pluginsManagerPool->setPmid(rec->value("pmid").toUInt());
	pluginsManagerPool->setAbsFilename(rec->value("abs_filename").toString());
	pluginsManagerPool->setFilename(rec->value("filename").toString());

	/* 加入缓存 */
	pluginsManagerPoolCache.insert("findOne" + spec.getSqlWhere(), pluginsManagerPool);

	return pluginsManagerPool;
}

QList<PluginsManagerPool> PluginsManagerPoolDao::findAll(const Specification &spec)
{
	if (pluginsManagerPoolsCache.contains("findAll" + spec.getSqlWhere()))
		return *pluginsManagerPoolsCache.object("findAll" + spec.getSqlWhere());

	QList<PluginsManagerPool> res;

	//构建sql语句
	QString content = QString("select * from `plugins_manager_pool` %1").arg(
				(spec.getSqlWhere() == "" ? "" : spec.getSqlWhere()));

	//获得查询结果
	QList<QSqlRecord> rec = ConnectionPool::runAll(content);

	//将查询的结果存入QList<PluginsManagerPool>链表中
	for(auto it = rec.begin(); it != rec.end(); it++)
	{
		PluginsManagerPool pluginsManagerPool;
		pluginsManagerPool.setId(it->value("id").toUInt());
		pluginsManagerPool.setPid(it->value("pid").toUInt());
		pluginsManagerPool.setPmid(it->value("pmid").toUInt());
		pluginsManagerPool.setAbsFilename(it->value("abs_filename").toString());
		pluginsManagerPool.setFilename(it->value("filename").toString());
		res.push_back(pluginsManagerPool);
	}

	/* 加入缓存 */
	pluginsManagerPoolsCache.insert("findAll" + spec.getSqlWhere(), &res);

	return res;
}

/* 保存 */
void PluginsManagerPoolDao::save(const PluginsManagerPool *pluginsManager)
{
	//先判断该插件管理器连接池数据是否存在，以主键id来判断
	QString content = QString("select * from `plugins_manager_pool` where id = %1").arg(pluginsManager->getId());
	QSqlRecord* rec = ConnectionPool::runOne(content);

	content.clear();
	if(rec == nullptr)
	{   //如果不存在，则insert
		content = QString("insert into `plugins_manager_pool` values(null, %1, %2, %3, %4)").arg(
					pluginsManager->getPid()).arg(
					pluginsManager->getPmid()).arg(
					Specification::tranString(pluginsManager->getAbsFilename())).arg(
					Specification::tranString(pluginsManager->getFilename()));
		ConnectionPool::runNo(content);
	}
	else
	{   //如果存在，则update
		content = QString("update `plugins_manager_pool` set pid = %1, pmid = %2, abs_filename = %3, filename = %4 "
						  "where id = %5").arg(
					pluginsManager->getPid()).arg(
					pluginsManager->getPmid()).arg(
					Specification::tranString(pluginsManager->getAbsFilename())).arg(
					Specification::tranString(pluginsManager->getFilename())).arg(pluginsManager->getId());
		ConnectionPool::runNo(content);
	}

	/* 清理缓存 */
	pluginsManagerPoolCache.clear();
	pluginsManagerPoolsCache.clear();
}

/* 删除 */
void PluginsManagerPoolDao::del(const quint32 &id)
{
	//先判断该插件管理器连接池数据是否存在，以主键id来判断
	QString content = QString("select * from `plugins_manager_pool` where id = %1").arg(id);
	QSqlRecord* rec = ConnectionPool::runOne(content);

	content.clear();
	if(rec == nullptr)
	{   //判空
		qDebug() << "无此插件管理器连接池";
		return;
	}

	content = QString("delete from `plugins_manager_pool` where id = %1").arg(id);
	ConnectionPool::runNo(content);

	/* 清理缓存 */
	pluginsManagerPoolCache.clear();
	pluginsManagerPoolsCache.clear();
}
